package com.medicine.hbp.adapter;

import java.util.List;

import com.medicine.hbp.mob.OptionListVo;
import com.medicine.hbp.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

public class VoteAdapter extends BaseAdapter {
	/**
	 * 选项集合
	 */
	public List<OptionListVo> optionList;
	private LayoutInflater inflater;
	private Activity activity;
	/**
	 * 是否为多选
	 */
	private String type;

	public VoteAdapter(Activity activity, List<OptionListVo> optionList,
					   String type) {
		this.optionList = optionList;
		this.activity = activity;
		this.type = type;
		inflater = LayoutInflater.from(activity);
	}

	@Override
	public int getCount() {
		return optionList.size();
	}

	public String getoptionBoolean() {
		String idList="";
		for (int i = 0; i < optionList.size(); i++) {
			if (optionList.get(i).optionBoolean) {
				idList+=optionList.get(i).optionId+",";
			}
		}
		return idList;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View view, ViewGroup arg2) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.vote_item, null);
			holder.textView = (TextView) view.findViewById(R.id.vote_item_name);
			holder.image_vote = (ImageView) view.findViewById(R.id.image_vote);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		OptionListVo listVo = optionList.get(arg0);
		if (listVo != null) {
			holder.textView.setText(listVo.optionMsg);
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					for (int i = 0; i < optionList.size(); i++) {
						if(type.equals("N")){
							if (i == arg0) {
								optionList.get(i).optionBoolean = true;
							} else {
								optionList.get(i).optionBoolean = false;
							}
						}else if(type.equals("Y")){
							if (i == arg0&&optionList.get(i).optionBoolean == false) {
								optionList.get(i).optionBoolean = true;
							}else if (i == arg0&&optionList.get(i).optionBoolean == true) {
								optionList.get(i).optionBoolean = false;
							}
						}

					}
					notifyDataSetChanged();
				}
			});
			if (listVo.optionBoolean) {
				holder.image_vote.setImageDrawable(activity.getResources()
						.getDrawable(R.mipmap.vote_1));
			} else {
				holder.image_vote.setImageDrawable(activity.getResources()
						.getDrawable(R.mipmap.vote_2));
			}
		}
		return view;
	}

	class ViewHolder {
		TextView textView;
		ImageView image_vote;
	}

}
